
package com.xwyzsn.rentcar.service;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xwyzsn.rentcar.entity.Notice;
import com.xwyzsn.rentcar.entity.Rentorder;
import com.xwyzsn.rentcar.mapper.NoticeMapper;
import com.xwyzsn.rentcar.mapper.RentorderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.lang.reflect.Field;
import java.util.Date;

@Service
public class NoticeService  {

    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private RentorderMapper rentorderMapper;


    public void autoRun(){
        List<Rentorder> data = rentorderMapper.selectList(null);
        data.forEach(rentorder -> {
            System.out.println(DateUtil.between(new Date(),rentorder.getEndTime(), DateUnit.DAY));

            if (DateUtil.between(new Date(),rentorder.getEndTime(), DateUnit.DAY)==0){

                Notice notice = new Notice();
                QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("id",rentorder.getId());
                if(noticeMapper.selectOne(queryWrapper)==null){
                    notice.setUserId(rentorder.getUserId());
                    String content = "您的订单"+rentorder.getId()+"已经到期,请及时还车";
                    notice.setContent(content);
                    notice.setId(rentorder.getId());
                    noticeMapper.insert(notice);
                }

            }

        });
    }

    public List<Notice> getOne(Map<String,?>payload){
        if (payload.isEmpty()){
            return noticeMapper.getAll();
        }
        Integer id = Integer.parseInt((String) payload.get("userId"));
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",id);
        queryWrapper.orderBy(true,false,"createdTime");
        return noticeMapper.selectList(queryWrapper);
    }

    public Boolean addOne(Map<String,?>payload){
        if (payload.isEmpty())return false;
        Notice notice = setEntity(payload,Notice.class);
        return noticeMapper.addOne(notice);
    }

    public Boolean deleteOne(Map<String,?>payload){
        Integer id = (Integer) payload.get("id");
        return noticeMapper.deleteOne(id);
    }

    public Integer updateOne(Map<String,?>payload){
        Notice notice = setEntity(payload,Notice.class);
        return noticeMapper.updateOne(notice);
    }

    public static <T> T setEntity(Map<String, ?> payload, Class<T> clazz) {
        try {
            T entity = clazz.newInstance();
            Field[] fields = clazz.getDeclaredFields();

            for (Field field : fields) {
                String fieldName = field.getName();
                if (payload.containsKey(fieldName)) {
                    field.setAccessible(true);
                    Object value = payload.get(fieldName);

                    // Handle type conversions if necessary
                    if (field.getType() == Integer.class && value instanceof Number) {
                        value = ((Number) value).intValue();
                    } else if (field.getType() == Float.class && value instanceof Number) {
                        value = ((Number) value).floatValue();
                    } else if (field.getType() == Date.class) {
                        value = (Date) value;
                    }else if (field.getType() == String.class && value instanceof String){
                        value = (String) value;
                    }
                    field.set(entity, value);
                }
            }
            return entity;
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace(); // Handle or log the exception as needed
            return null;
        }
    }
}

